# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
        ans = [None] * k
        length = 0
        tmp = head
        while tmp:
            length += 1
            tmp = tmp.next
        list1 = [0] * k
        index = 0
        k1 = k
        while length > 0:
            val1 = length // k1
            val2 = length % k1
            if val2 == 0:
                list1[index] = max(1, val1)
                length -= max(1, val1)
            else:
                list1[index] = val1 + 1
                length -= (val1 + 1)
            index += 1
            k1 -= 1
        pre = None
        cur = head
        count = 0
        index = 0
        first = head
        index1 = 0
        while cur:
            count += 1
            if count == list1[index]:
                pre = cur
                cur = cur.next
                index += 1
                count = 0
                ans[index1] = first
                index1 += 1
                first = cur
                pre.next = None
            else:
                pre = cur
                cur = cur.next
        return ans